<style type="text/css">
	table img{
		cursor:pointer
	}
</style>

<div class="card">
	<div class="card-content">
		<div class = 'row row-head' >
			<div class = 'col s12'>
				<div class="btn-group left  margin-right-100" role="group" >
					<button class = 'btn btn-default btn-default-1' id = 'check'>  <i class="material-icons text-color5"  >create</i> 审核</button>
					<button class = 'btn btn-default btn-default-1' id = 'flow' >  <i class="material-icons text-color3"  >repeat</i> 流程</button>
					<button class = 'btn btn-default btn-default-1' id = 'retrieve' >  <i class="material-icons text-color4"  >keyboard_backspace</i> 取回</button>
					<button class = 'btn btn-default btn-default-1' id = 'search'> <i class="material-icons text-color1"  >search</i> 搜索</button>
				</div>
				<div class = 'relative left'>
					<input type = 'text' id = 'search_table' class = 'width-400' placeholder = '表单内搜索' />
					<img src = '__PUBLIC__/image/search.png' class = 'absolute height18 input-hint' />
				</div>
			</div>
		</div>

		<div class = 'row row-body'>
			<div class = 'col s12'>
				<table class = 'dataTable centered table-small dataTable centered table-gray-header cell-border stripe' id = 'table'>
					<thead>
						<tr><th></th><th>标题</th><th>发起人</th><th>发起时间</th><th>接受时间</th><th>上一处理人</th><th>类型</th><th>当前处理人</th><th>处理状态</th><th>流程日志</th></tr>
					</thead>
					<tbody id = 'tbody'>
						{$tbody|raw}
					</tbody>
				</table>
			</div>
		</div>

		{include file='public/page/page' /}
	
	
					
	</div>
</div>
<style type="text/css">
	
</style>

<script>

	

	var app = {
		
		table : {},

		parameterO: {},

		searchOption : {},
		check : function( exc = '' ){
			let index1 = layer.load(2);
			let obj = {};
			obj.flowid = $('#tbody tr.selected').eq(0).children().eq(1).find('a').eq(0).data('flow_id');
			obj.id = $('#tbody tr.selected').eq(0).children().eq(1).find('a').eq(0).data('id');
			obj.nodeid = $('#tbody tr.selected').eq(0).children().eq(1).find('a').eq(0).data('node_id');
			if(exc != '') obj.executor = exc;
			$.post('__APP__/Fs/check',obj,function(d){
				layer.close(index1);
				if(d.status == 's'){
					$('#tbody tr.selected').eq(0).children().eq(8).find('img').eq(0).attr('src', '/a1/index/view/public/Image/o10.png').attr('title','已处理');;
					layer.msg('审核成功',{icon:1,time:1000,offset:'30%'});
				}else if(d.status == 'm'){
					app.tmp = new Map();
					let c = "<div style = 'font-size:12px;'>";
					for(let i in d.data){
						let zxmsName = '';
						if(d.data[i]['zxms'] == 1) zxmsName = '单人执行';
						if(d.data[i]['zxms'] == 2) zxmsName = '多人执行';
						c += "<div class = 'row' style = 'margin:0;padding:20px 10px;border-bottom:1px solid #bfbfbf'>"
						c += "<div class = 'col s3 hint1'>"+d.data[i]['name']+"</div><div class = 'col s3'>"+zxmsName+"</div><div class = 'col s6'><input placeholder = '点击选择执行人' id = '"+i+"' data-id = '"+i+"'  data-zxms = '"+d.data[i]['zxms']+"' type = 'text' class = 'aya-input xzfzclr' style = 'height:24px' readonly /></div>";
						c += "</div>";
						app.tmp.set(i,d.data[i]['list']);
					}

					c += "</div>";		

					let index = parent.layer.open({
						title : '<span style = "font-size:12px">选择分支处理人</span>',
						offset : ['48px'],
						area  : ['900px','660px'],
						type  : 1,
						btn   : ['确定'], //aya_mom_moallocate_change
						shadeClose:true,
						content : c,
						yes : function(){
							let a = [];
							let allSelected = true;
							parent.$('input.xzfzclr').each(function(){
								let tmp = {};
								tmp.id = $(this).data('id');
								if($(this).data('executor') == undefined || $(this).data('executor') == '[]'){
									allSelected = false;
									return false;
								}else{
									tmp.executor = $(this).data('executor');
								}
								a.push(tmp);
							});

							if(allSelected == false){
								layer.msg('请选择执行人',{icon:2,time:1500,offset:'30%'});
								return false;
							}
							app.check(JSON.stringify(a));
							layer.close(index);

						},
						success : function(layero, index){
							parent.$('input.xzfzclr').click(function(){

								let t = $(this).parent().prev().prev().text();

								let id = $(this).data('id');

								app.mSelected = id;

								let zxms = $(this).data('zxms');


								let n = "<div class = 'row' style = 'margin:0'><div class = 'col s12' id = 'top123'><div style = 'display:inline-block;width:50%;position:relative'><input id = 'i20191119' placeholder = '按回车搜索' type = 'text' class = 'aya-input' style = 'height:24px;width:100%' /><img class = 'icon4' src = '/a1/index/view/public/Image/o25.png' style = 'position:absolute;right:0;top:3px;'/></div></div><div class = 'col s12'><table id = 't20191119' class = 'centered dataTable row-border noselect table-small'><thead><tr></tr><tr><th></th><th>工号</th><th>姓名</th></tr></thead><tbody>";

								let selected = new Map();
								if(parent.$('#' + app.mSelected).data('executor') != undefined){
									JSON.parse(parent.$('#' + app.mSelected).data('executor')).forEach(function(v,k){
										selected.set(v.k,1);	
									});
								}
								
								let tmp1 = '',tmp2 = '';
								
								app.tmp.get(id).forEach(function(v,k){
									if(selected.get(v.k) == undefined){
										tmp1 += "<tr><td><input type = 'checkbox' class = 'aya-checkbox' /></td><td>"+v.k+"</td><td>"+v.v+"</td></tr>";
									}else{
										tmp2 += "<tr class = 'selected'><td><input type = 'checkbox' class = 'aya-checkbox' checked /></td><td>"+v.k+"</td><td>"+v.v+"</td></tr>";
									}
										
								});

								n = n + tmp2 + tmp1;

								n += "</tbody></table></div><div class = 'col s12 center' style = 'padding:8px' ><button class = 'btn btn-primary height32' id = 'btn20191119'>确定</button></div></div>";


						
								parent.layer.open({
									title : '<span class = "hint2" style = "font-size:12px">选择分支处理人（'+t+'）</span>',
									offset : ['48px'],
									area  : ['500px','660px'],
									type  : 1,
									shadeClose:false,
									content: n,
									success : function(layero2, index2){
										
										let scrollY = 562 - $('#top123').height()  - top.$('#btn20191119').parent().height();
										if(top.mainPage.multiType == 1) scrollY -= 26;

										let setting = {
											paging: false,
											scrollY: scrollY ,
											info:false,
											ordering:false,
											dom:'t',	
										};

										let t20191119 = parent.$('#t20191119').DataTable(setting);

										if(zxms == 1){
											parent.select_tr('t20191119');
										}else{
											parent.select_tr2('t20191119');
										}
										

											
										parent.$('#i20191119').keypress(function(e){
										
											let text = $.trim(parent.$('#i20191119').val());
											
											if(e.keyCode == 13){
												t20191119.search(text).draw();
											}
										});
											
										parent.$('#btn20191119').click(function(){
											let a = [];
											parent.$('#t20191119 tbody tr.selected').each(function(){
												let tmp = {};
												tmp.k = $(this).children().eq(1).text();
												tmp.v = $(this).children().eq(2).text();
												a.push(tmp);
											});
											if(a.length == 0){
												layer.msg('请选择执行人',{icon:2,time:1500,offset:'30%'});
											}else{
												parent.$('#'+app.mSelected).data('executor',JSON.stringify(a));
												let tmp = ''
												for(let i = 0; i < a.length; i++){
													if( i == a.length - 1){
														tmp += a[i]['v'];
													}else{
														tmp = tmp + a[i]['v'] + ',';
													}
												}
												$('#'+app.mSelected).val(tmp);
												parent.layer.close(index2);
											}
										});

									}
										
								});

							});

						},
					});
						

				}else{
					layer.alert(d.info,{shadeClose:true,'title' : "<span style = 'font-size:12px'>错误信息</span>",area:['500px'],icon:2,offset:'20%'});
				}		
			});

		},




		ini : function(){

			app.table = d_table();

			$('#check').click(function(){
				if($('#tbody tr.selected').length == 0) return false;
				app.check()
			});
		
			$('#retrieve').click(function(){
				if($('#tbody tr.selected').length == 0) return false;
				layer.confirm('确定取回此流程？',{title : '提示',offset:['10%']},function(index){
					let index2 = top.layer.load(2);
					$.post('__APP__/Fs/retrieve',app.parameterO,function(d){
						top.layer.close(index2);
						if(d.status == 's'){
							app.table.row($('#tbody tr.selected').eq(0)).remove().draw();
							layer.msg('回退成功',{icon:1,time:1000,offset:'30%'});
						}else{
							layer.msg(d.info,{icon:2,time:2000,offset:'30%'});
						}
					});
				});
			});

			$('#flow').click(function(){

				if($('#tbody tr.selected').length == 0) return false;

				parent.layer.open({
					title:'<span style = "font-size:12px">流程查看</span>',
					type: 2,
					shadeClose:true,
					offset:'0',
					area: ['90%','100%'],
					content: "__APP__/Fs/see?flowid="+app.parameterO.flowid,
					isOutAnim: false,
				});

			});


			$('#tbody').click(function(e){

				let flow_id,node_id,id;
				
				
				if(e.target.nodeName.toUpperCase() == 'TR'){
					flow_id = $(e.target).data('flow_id');
					node_id = $(e.target).data('node_id');
					id = $(e.target).data('id');
				}else{
					
					flow_id = $(e.target).parents('tr').data('flow_id');
					node_id = $(e.target).parents('tr').data('node_id');
					id = $(e.target).parents('tr').data('id');

				}

				
				app.parameterO.flowid = flow_id;
				app.parameterO.nodeid = node_id;
				app.parameterO.id = id;
			
				if(e.target.nodeName == 'A'){
					parent.layer.open({
						skin: 'layer-edit-container',
						title:'<span style = "font-size:12px">流程处理</span>',
						type: 2 ,
						shadeClose:true ,
						offset:'0' ,
						area: [ '1200px' , '100%' ],
						content: '__APP__/Fs/flow2?flow_id='+flow_id+'&node_id='+node_id+'&id='+id,
						isOutAnim: false,
						success : function(layero, index){
							top.tmp2 = layero.find('iframe')[0]['name'];
							
						},
					});
				}else if($(e.target).hasClass('rizhi')){
					let flow_id = $(e.target).data('flow_id');
					top.layer.open({
						title:'<span style = "font-size:12px">流程日志</span>',
						area: ['800px','100%'],
						offset:'1px',
						shadeClose:true,
						isOutAnim: false ,
						maxmin: true,
						type: 2, 
						content:'__APP__/Fs/flow_log?flow_id='+flow_id
					});
				}
			});




		

			

			$('#search_table').keypress(function(e){
				let text = $.trim($(this).val());
				if(e.keyCode == 13){
					table.search(text).draw();
				}
			});

			page('__APP__/Fs/research',app.table,$.extend({type:3},app.searchOption));

			$('#search').click(function(){
				let div2 = [
					{ 'name' : '发起者编号：' ,'id' : 'username' ,'type' : 'text'},
					{ 'name' : '发起者姓名：' ,'id' : 'maker_name' ,'type' : 'text'},
					{ 'name' : '标题：' ,'id' : 'title' ,'type' : 'text'},
				];
				parent.layer.open({
					skin: 'layer-search-container',
					title : '搜索用户',
					offset : ['40px'],
					area  : ['600px',"300px"],
					type  : 1,
					btn   : ['搜索','清除'],
					shadeClose:true,
					isOutAnim: false,
					content : create_new_div( div2 ),
					success : function(){
						for(let s of div2){
							$('#' + s.id).val( app.searchOption[s.id] );
						}
					},
					btn2 : function(){
						for(let s of div2){
							$('#' + s.id).val('');
						}
						return false;
					},
					yes : function(index){
						let o = get_page();
						let hasOption = false;
						for(let s of div2){
							o[s.id] = $('#' + s.id).val();
							app.searchOption[s.id] = $('#' + s.id).val();
							if(app.searchOption[s.id] != '') hasOption = true;
						}
						o.type = 1;
						if(hasOption){
							$('#search').addClass('has-option');
						}else{
							$('#search').removeClass('has-option');
						}
						page_callback(o,'__APP__/Fs/research',app.table);
						parent.layer.close(index);
					}
				});
			});


		},	

	}
	app.ini();


	//$('#table tbody tr').eq(0).trigger('click');
	//$('#flow').trigger('click');
</script>



